#include "loginview.h"
#include <QQmlEngine>
#include <QSystemTrayIcon>
#include <QMenu>
#include <QAction>
#include "configini.h"
#include <QDebug>
#include "httpnet.h"
#include "loginmodel.h"
#include <QQmlContext>
#include <QtNetwork/QNetworkAccessManager>


LoginView::LoginView(QQuickView *parent):QQuickView(parent)
{
    //setFlags(Qt::FramelessWindowHint|Qt::Tool);
    setFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowMinimizeButtonHint| Qt::Window);
    setColor(Qt::transparent);
    setResizeMode(QQuickView::SizeRootObjectToView);
    setWidth(450);
    setHeight(450);
    setMaximumWidth(450);
    setMaximumHeight(450);
    setMinimumHeight(450);
    setMinimumWidth(450);
    mHelper = new DDFramelessHelper(this);
    mHelper->activateOnQmlWidget(this);
    mHelper->setTitleHeight(100);
    mHelper->setWidgetResizable(false);
    setIcon(QIcon(":/images/360logo.ico"));
    setTitle("中国必印网文件自动下载器 V1.0");




}
void LoginView::showTrayIcon(){
    QObject *root = 0;
    if (rootObject() != NULL)
    {
        root = rootObject();
        QAction *minimizeAction = new QAction(QObject::tr("Mi&nimize"), this);
        root->connect(minimizeAction, SIGNAL(triggered()), this, SLOT(hide()));
        QAction *maximizeAction = new QAction(QObject::tr("Ma&ximize"), this);
        root->connect(maximizeAction, SIGNAL(triggered()), this, SLOT(showMaximized()));
        QAction *restoreAction = new QAction(QObject::tr("&Restore"), this);
        root->connect(restoreAction, SIGNAL(triggered()), this, SLOT(showNormal()));
        QAction *quitAction = new QAction(QObject::tr("&Quit"), this);
        root->connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));

        QMenu *trayIconMenu = new QMenu();
        trayIconMenu->addAction(minimizeAction);
        trayIconMenu->addAction(maximizeAction);
        trayIconMenu->addAction(restoreAction);
        trayIconMenu->addSeparator();
        trayIconMenu->addAction(quitAction);

        QSystemTrayIcon *trayIcon = new QSystemTrayIcon(this);
        trayIcon->setContextMenu(trayIconMenu);
        trayIcon->setToolTip("中国必印网文件自动下载器 V1.0");
        trayIcon->setIcon(QIcon(":/images/360logo.ico"));
        trayIcon->show();
    }

}
/// param url
/// param regsiter
/// 加载主qml界面
/// 并且注册C++对象到qml，方便与qml进行交互
///
void LoginView::setSourceAndRegsiterObj(const QUrl &url, bool regsiter)
{
    if(regsiter){
         rootContext()->setContextProperty("loginCtrl", this);
         setSource(url);
        //setSource后才能得到obj对象
        m_Obj  = this->rootObject();
        connect(m_Obj,SIGNAL(signalQmlOptions(QVariant,QString)),this,SLOT(slotRecevQmlReq(QVariant,QString)));
        connect(m_Obj,SIGNAL(signalLoginEvent(QString,QString)),this,SLOT(loginEventQml(QString,QString)));

    }
}

void LoginView::loginEventQml(QString username,QString password){
     qDebug() << username << password;
     HttpNet *httpnet = new HttpNet();
     httpnet->http = new QNetworkAccessManager(httpnet);
     connect(httpnet->http,SIGNAL(finished(QNetworkReply*)),this,SLOT(replyfinished(QNetworkReply*)));
     ConfigIni *config = new ConfigIni(qApp->applicationDirPath()+"/","config.ini");
     qDebug() << "读取INI配置";
     qDebug() << config->GetIni("server","host")+":"+config->GetIni("server","port")+"/member/auth/user/login";

     httpnet->post(config->GetIni("server","host")+":"+config->GetIni("server","port")+"/member/auth/user/login","type=mobile&mobile="+username+"&password="+password);
}
void LoginView::replyfinished(QNetworkReply *reply)//每当执行网站应答结束后，就会执行该槽函数
{
    QVariant status_code=reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
    qDebug()<<"code_state="<<status_code;//网络状态,200代表正常，302代表重定向，404：not found等等
    if(status_code == QVariant::Invalid){
        qDebug()<<"没有连接上地址";
    }else{
        qDebug()<<"连接上地址";
    }

     QByteArray bodys;
     bodys = reply->readAll();
     QString    str(bodys);
     emit loginSignal(bodys);

    //qDebug()<<"返回的数据"<<bodys;
}
/// param var
/// param cmd
/// 接受来自QML的请求
///signalQmlOptions
void LoginView::slotRecevQmlReq(QVariant var, QString cmd)
{
    if(var.toString()=="system"){
        if(cmd == "close"){
            this->close();
            qApp->quit();
        }else if(cmd == "min"){
            this->showMinimized();
        }
    }

}


///
/// \brief DDNewQuickWindow::mouseDoubleClickEvent
/// \param event
/// 双击可最大化窗口和还原窗口大小
///
//void LoginView::mouseDoubleClickEvent(QMouseEvent *event)
//{
//    QRect rect(10,2,this->width()-70,mHelper->titleHeight());

//    if(event->button() == Qt::LeftButton && rect.contains(event->pos())) {
//        if(!isMax){
//            isMax = !isMax;
//            slotShowMaxAndNormal(true);
//            showMaximized();
//            update();
//        }else{
//            isMax = !isMax;
//            slotShowMaxAndNormal(false);
//            showNormal();
//            update();
//        }
//    }
//    QQuickView::mouseDoubleClickEvent(event);
//}

/// \brief YbQuickView::slotShowMaxAndNormal
/// \param showMax
///  你可以注册该方法到QML当中
void LoginView::slotShowMaxAndNormal(bool showMax)
{
    if(m_Obj){
        QVariant returnVal;
        QMetaObject::invokeMethod(m_Obj,"transToQml",Q_RETURN_ARG(QVariant,returnVal),
                                  Q_ARG(QVariant,showMax));
    }
}

